package com.sjh.lib_app

import androidx.lifecycle.Lifecycle
import com.sjh.baseutils.loading.LoadingType
import com.sjh.baseutils.toast
import com.sjh.lib_app.bean.Request
import com.sjh.lib_app.bean.UserBean
import com.sjh.lib_app.net.NetConfig
import com.sjh.net.Method
import com.sjh.net.request

/**
 *    author : SunJH
 *    date   : 2021/4/13 0013
 *    desc   :
 */
object CommRequest {
    /**
     * 登录
     */
    fun login(mLifecycle: Lifecycle, loadingType: LoadingType = LoadingType.GENERAL,time:Long = 30*1000, account:String, password:String,
              onErrorCall: (Throwable) -> Unit, onresult: (request: Request<UserBean>) -> Unit){
        request<Request<UserBean>>(preBlock = { NetConfig.appConfig(this) }, options = {
            method = Method.POST
            lifecycle = mLifecycle
        }) {
            url = "/api/login/login-user"
            connectTimeout = time
            params = {
                "type" to "1"
                "checkPass" to password
                "mobile" to account
            }
            loading = loadingType
            onError = {
                onErrorCall.invoke(it)
            }
            onResult = { s: String, request: Request<UserBean>, b: Boolean, i: Int ->
                onresult.invoke(request)
            }
        }
    }

    /**
     * 发送验证码
     *  register-注册
     *  forger-找回密码
     */
    fun requestCode(mLifecycle: Lifecycle,phone:String,type:String,result:(sendCode:Boolean)->Unit){
        request<Request<Any>>(preBlock = {NetConfig.appConfig(this)},options = {
            method = Method.POST
            lifecycle = mLifecycle
        }) {
            url = "/api/ali-message/send"
            params = {
                "type" to type
                "phone" to phone
            }
            onError = {
                result.invoke(false)
            }
            onResult = { s: String, request: Request<Any>, b: Boolean, i: Int ->
                if(request.isSuccess){
                 result.invoke(true)
                }else{
                    (request.msg?:"获取验证码失败").toast()
                }
            }
        }
    }
}